<template>
  <div>
    <GQuerySearch placeholder="请输入姓名" @setValue="setQuery" />
    <GTable
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      usePager
      :pager="frontPager"
      :data="tableData"
      stripe
      border
      :columns="columns"
      :conf="tableConf"
    >
      <template #status="{ row }">
        <el-tag :type="row.status | statusColor">{{
          row.status | status
        }}</el-tag>
      </template>
      <template #operation="{ row }">
        <el-button
          type="primary"
          :disabled="row.status !== 5"
          @click="genContract(row.id)"
          >生成合同</el-button
        >
        <el-button
          type="success"
          :disabled="row.status !== 7"
          @click="downloadFile(row.id)"
          >下载合同</el-button
        >
      </template>
    </GTable>
  </div>
</template>

<script>
import { columns, tableConf } from './pageConf'
import pager from '@/mixins/pager'
import crud from '@/mixins/crud'
import {
  makeContract,
  downloadContractUrl,
  downloadContract,
} from '@/api/loan'
import { downloadByTag } from '@/utils'
// import axios from 'axios'
// import req from '@/utils/request'
export default {
  name:'contract',
  mixins: [pager, crud],
  beforeDestroy(){
    console.log(this.$options.name+"被销毁了")
  },
  data() {
    return {
      columns,
      tableConf,
    }
  },
  created() {
    this.load()
  },
  methods: {
    beforeInit() {
      this.loadUrl = '/contract/list'
      return true
    },
    async genContract(id) {
      let isGen = await this.$gConfirm('确定生成合同吗?')
      if (!isGen) return
      let res = await makeContract(id)
      this.$message.success('生成合同成功')
      this.load()
    },
    async downloadFile(id) {
      let isDownload = await this.$gConfirm('确定下载合同吗?')
      if (!isDownload) return
      let res = await downloadContractUrl(id)

      if (!res?.data?.data?.url) throw new Error('无法获取Url')

      const downloadUrl = res?.data?.data?.url.replace('/api', '')

      let resBlob = await downloadContract(downloadUrl)
      console.log(resBlob, 'resBlob')
      // req(
      //   {
      //     url:downloadUrl,
      //     responseType: 'blob', // arrayBuffer blob 浏览器常见的文件
      //   },
      //   { fullLoading: true }
      // ).then(res=>{
      //   console.log('resBlob',res);
      // })
      // axios({
      //   url: res?.data?.data?.url,
      //   responseType: 'blob', // arrayBuffer blob 浏览器常见的文件
      // }).then((res) => {
      //   console.log('resBlob', res)
      // })
      await downloadByTag(resBlob.data, '贷款合同-' + id + '.docx')

      this.$message.success("下载合同成功");
    },
  },
}
</script>
